home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 37 / IOPROG_37.ISO / SOFT / Multilizer.exe / disk1 / data1.cab / data1 / [Group9]VCL Source Standard / ivfilted.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-08-12  |  4.3 KB  |  209 lines

  1. unit IvFiltEd;
  2.  
  3. {$I IVMULTI.INC}
  4.  
  5. interface
  6.  
  7. uses
  8. {$IFDEF WIN32}
  9.   Windows,
  10. {$ELSE}
  11.   WinTypes, WinProcs,
  12. {$ENDIF}
  13.   Messages, Classes, Graphics, Forms, Controls, Tabs,
  14.   Buttons, DsgnIntf, Grids, StdCtrls, ExtCtrls;
  15.  
  16. type
  17.   TIvFilterEditor = class(TForm)
  18.     Bevel1: TBevel;
  19.     OKButton: TButton;
  20.     CancelButton: TButton;
  21.     HelpButton: TButton;
  22.     procedure FormCreate(Sender: TObject);
  23.     procedure HelpButtonClick(Sender: TObject);
  24.  
  25.   private
  26.     procedure SetFilter(value: String);
  27.     function GetFilter: String;
  28.   end;
  29.  
  30.   TIvFilterProperty = class(TStringProperty)
  31.   public
  32.     procedure Edit; override;
  33.     function GetAttributes: TPropertyAttributes; override;
  34.   end;
  35.  
  36. implementation
  37.  
  38. uses
  39.   SysUtils, LibHelp;
  40.  
  41. {$R *.DFM}
  42.  
  43. const
  44.   NAME_COL_C = 0;
  45.   FILT_COL_C = 1;
  46.   FIRST_ROW_C = 1;
  47.  
  48. type
  49.   TFilterGrid = class(TStringGrid)
  50.   private
  51.     FLimit: Integer;
  52.     
  53.   public
  54.     function TotalChars: Integer;
  55.     function GetEditLimit: Integer; override;
  56.   end;
  57.  
  58. var
  59.   filterGrid: TFilterGrid;
  60.  
  61.  
  62. { TFilterGrid }
  63.  
  64. function TFilterGrid.TotalChars: Integer;
  65. var
  66.   r: Integer;
  67. begin
  68.   Result := 0;
  69.   for r := FIRST_ROW_C to RowCount - 1 do
  70.     Result := Result + (Length(Cells[NAME_COL_C, r]) + Length(Cells[FILT_COL_C, r]));
  71. end;
  72.  
  73. function TFilterGrid.GetEditLimit: Integer;
  74. begin
  75.   Result := (FLimit - TotalChars) + Length(Cells[Col, Row]);
  76.   if Result = 0 then
  77.     Result := -1; { sets cell to read only }
  78. end;
  79.  
  80.  
  81. { TFilterEditor }
  82.  
  83. procedure TIvFilterEditor.FormCreate(Sender: TObject);
  84. begin
  85.   filterGrid := TFilterGrid.Create(Self);
  86.   filterGrid.BoundsRect := Bevel1.BoundsRect;
  87.   with filterGrid do
  88.   begin
  89.     ColCount := 2;
  90.     FixedCols := 0;
  91.     Font.Name := 'MS Sans Serif';
  92.     Font.Size := 8;
  93.     Font.Style := [];
  94.     RowCount := 25;
  95.     ScrollBars := ssVertical;
  96.     Options := [goFixedVertLine, goHorzLine, goVertLine, goEditing, goTabs, goAlwaysShowEditor];
  97.     FLimit := 240;
  98.     Parent := Self;
  99.     TabOrder := 1;
  100.     ColWidths[NAME_COL_C] := ClientWidth div 2;
  101.     ColWidths[FILT_COL_C] := (ClientWidth div 2) - 1;
  102.     DefaultRowHeight := Canvas.TextHeight('A') + 2;
  103.     Cells[NAME_COL_C,0] := 'Filter Name';
  104.     Cells[FILT_COL_C,0] := 'Filter';
  105.   end;
  106.   ActiveControl := FilterGrid;
  107. end;
  108.  
  109. function TIvFilterEditor.GetFilter: string;
  110.  
  111.   function EmptyRow(r: Integer): Boolean;
  112.   begin
  113.     Result := True;
  114.     with FilterGrid do
  115.       if (Cells[NAME_COL_C,r] <> '') or (Cells[FILT_COL_C,r] <> '') then
  116.         Result := False;
  117.   end;
  118.  
  119. var
  120.   r: Integer;
  121. begin
  122.   Result := '';
  123.   with FilterGrid do
  124.   begin
  125.     for r := FIRST_ROW_C to RowCount-1 do
  126.     begin
  127.       if not EmptyRow(r) then
  128.       begin
  129.         Result := Result + Cells[NAME_COL_C, r];
  130.         Result := Result + '|';
  131.         Result := Result + Cells[FILT_COL_C, r];
  132.         Result := Result + '|';
  133.       end;
  134.     end;
  135.   end;
  136.  
  137.   r := Length(Result);
  138.   while Result[r] = '|' do
  139.   begin
  140.     Delete(Result, r, 1);
  141.     Dec(r);
  142.   end;
  143. end;
  144.  
  145. procedure TIvFilterEditor.SetFilter(Value: string);
  146. var
  147.   Index: Byte;
  148.   r, c: Integer;
  149. begin
  150.   if Value <> '' then
  151.   begin
  152.     r := FIRST_ROW_C;
  153.     c := NAME_COL_C;
  154.     Index := Pos('|', Value);
  155.     with FilterGrid do
  156.     begin
  157.       while Index > 0 do
  158.       begin
  159.         Cells[c, r] := Copy(Value, 1, Index - 1);
  160.         if c = FILT_COL_C then
  161.         begin
  162.           c := NAME_COL_C;
  163.           if r = RowCount - 1 then
  164.             RowCount := RowCount + 1;
  165.           r := r + 1;
  166.         end
  167.         else c := FILT_COL_C;
  168.         Delete(Value, 1, Index);
  169.         Index := Pos('|', Value);
  170.       end;
  171.       Cells[c, r] := Copy(Value, 1, Length(Value));
  172.     end;
  173.   end;
  174. end;
  175.  
  176. procedure TIvFilterEditor.HelpButtonClick(Sender: TObject);
  177. begin
  178.   Application.HelpContext(HelpContext);
  179. end;
  180.  
  181. { TIvFilterProperty }
  182.  
  183. procedure TIvFilterProperty.Edit;
  184. var
  185.   filterEditor: TIvFilterEditor;
  186. begin
  187.   filterEditor := TIvFilterEditor.Create(Application);
  188.   try
  189.     filterEditor.SetFilter(GetValue);
  190.     filterEditor.ShowModal;
  191.     if filterEditor.ModalResult = mrOK then
  192.       SetValue(filterEditor.GetFilter);
  193.   finally
  194.     filterEditor.Free;
  195.   end;
  196. end;
  197.  
  198. function TIvFilterProperty.GetAttributes: TPropertyAttributes;
  199. begin
  200.   Result := [paDialog
  201. {$IFDEF WIN32}
  202.     , paRevertable
  203. {$ENDIF}
  204.   ];
  205. end;
  206.  
  207. end.
  208.  
  209.